<template>
  <view class="leave-container">
    <!-- 顶部导航栏 -->
    <nav-bar title="租客合同" :showSearch="true" @search="showSearch"></nav-bar>
    
    <!-- 筛选条件区域 -->
    <filter-bar
      status-text="在租中"
      @filter="toggleFilter"
    ></filter-bar>
    
    <!-- 合同列表 -->
    <scroll-view scroll-y class="contract-list" @scrolltolower="loadMore">
      <view class="contract-item" v-for="(item, index) in contractList" :key="index">
        <view class="status-tag">在租中</view>
        <view class="contract-content">
          <view class="contract-info">
            <view class="contract-title">{{item.title}}</view>
            <view class="contract-detail">
              <text class="date">{{item.start_date}} 至 {{item.end_date}}</text>
              <text class="tenant">| {{item.tenant}}</text>
            </view>
            <view class="contract-status">
              <text class="days-left">{{item.days_left}}天后离店</text>
            </view>
          </view>
          <view class="contract-action">
            <view class="bind-btn" @click="inviteBind(item)">
              <text>邀请绑定</text>
            </view>
          </view>
        </view>
      </view>
      
      <!-- 加载更多 -->
      <view class="loading" v-if="isLoading">
        <text>加载中...</text>
      </view>
      <view class="no-more" v-if="noMore">
        <text>没有更多数据了</text>
      </view>
    </scroll-view>
  </view>
</template>

<script>
import NavBar from '@/components/nav-bar/index.vue';
import FilterBar from '@/components/filter-bar/index.vue';

export default {
  components: {
    NavBar,
    FilterBar
  },
  data() {
    return {
      contractList: [
        {
          title: '光谷青年城9号1单元3214·1',
          department: '总部 | 杨子维',
          start_date: '2023-08-23',
          end_date: '2024-02-22',
          tenant: '杨子维',
          days_left: 1,
          tag: ''
        },
        {
          title: '明珠花园5号3单元701·1',
          department: '总部 | 庞楚杨',
          start_date: '2024-02-23',
          end_date: '2025-02-22',
          tenant: '庞楚杨',
          days_left: 1,
          tag: ''
        },
        {
          title: '光谷悦城9号2单元1704·2',
          department: '总部 | 刘幸宇',
          start_date: '2024-02-24',
          end_date: '2025-02-23',
          tenant: '刘幸宇',
          days_left: 2,
          tag: ''
        },
        {
          title: '保利华都28号1单元3003·3',
          department: '总部 | 陈红林',
          start_date: '2023-11-24',
          end_date: '2024-02-23',
          tenant: '陈红林',
          days_left: 2,
          tag: ''
        },
        {
          title: '芭比伦堡6号4单元102·2',
          department: '总部 | 瞿田',
          start_date: '2023-11-25',
          end_date: '2024-02-24',
          tenant: '瞿田',
          days_left: 3,
          tag: ''
        },
        {
          title: '中建大公馆16号2单元2202·2',
          department: '总部 | 袁友盟',
          start_date: '2023-07-25',
          end_date: '2024-02-24',
          tenant: '袁友盟',
          days_left: 3,
          tag: ''
        },
        {
          title: '金地中心城6号2单元204·3',
          department: '总部 | 史清玉',
          start_date: '2023-07-25',
          end_date: '2024-02-24',
          tenant: '史清玉',
          days_left: 3,
          tag: ''
        }
      ],
      isLoading: false,
      noMore: false,
      page: 1,
      pageSize: 10
    }
  },
  onLoad() {
    // 页面加载时获取合同数据
    this.getContractList();
  },
  methods: {
    goBack() {
      uni.navigateBack();
    },
    showSearch() {
      // 跳转到搜索页面
      uni.navigateTo({
        url: '/pages/contract/search'
      });
    },
    toggleFilter(type) {
      // 跳转到对应的筛选页面
      uni.navigateTo({
        url: `/pages/contract/filter?type=${type}`
      });
    },
    toggleSort() {
      // 跳转到排序页面
      uni.navigateTo({
        url: '/pages/contract/sort'
      });
    },
    getContractList(refresh = false) {
      // 获取合同列表数据
      this.isLoading = true;
      // 这里应该调用实际的API获取数据
      setTimeout(() => {
        if (refresh) {
          this.contractList = [];
        }
        // 模拟获取数据
        const hasMoreData = false; // 根据实际数据情况判断是否还有更多数据
        this.noMore = !hasMoreData;
        this.isLoading = false;
      }, 1000);
    },
    loadMore() {
      if (!this.isLoading && !this.noMore) {
        this.getContractList();
      }
    },
    inviteBind(item) {
      // 跳转到邀请绑定页面
      uni.navigateTo({
        url: `/pages/guide/index?contract_id=${item.id}&type=bind`
      });
    }
  }
}
</script>

<style lang="scss">
.leave-container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.nav-bar {
  display: flex;
  align-items: center;
  height: 90rpx;
  background-color: #fff;
  padding: 0 30rpx;
  position: relative;
  
  .nav-left {
    position: absolute;
    left: 30rpx;
    height: 100%;
    display: flex;
    align-items: center;
  }
  
  .nav-title {
    flex: 1;
    text-align: center;
    font-size: 32rpx;
    font-weight: bold;
  }
  
  .nav-right {
    position: absolute;
    right: 30rpx;
    height: 100%;
    display: flex;
    align-items: center;
  }
}

.filter-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #fff;
  padding: 20rpx 30rpx;
  border-bottom: 1rpx solid #eee;

  .sort-item {
    position: relative;
    padding-right: 30rpx;
    
    &::after {
      content: '';
      position: absolute;
      right: 0;
      top: 50%;
      width: 0;
      height: 0;
      border-left: 6rpx solid transparent;
      border-right: 6rpx solid transparent;
      border-bottom: 6rpx solid #666;
      transform: translateY(-8rpx);
    }
    
    &::before {
      content: '';
      position: absolute;
      right: 0;
      top: 50%;
      width: 0;
      height: 0;
      border-left: 6rpx solid transparent;
      border-right: 6rpx solid transparent;
      border-top: 6rpx solid #666;
      transform: translateY(2rpx);
    }
  }
  
  .filter-item {
    display: flex;
    align-items: center;
    font-size: 24rpx;
    color: #333;
    padding: 0 20rpx;
    position: relative;
    
    &:not(:last-child)::after {
      content: '';
      position: absolute;
      right: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 1rpx;
      height: 24rpx;
      background-color: #eee;
    }
    
    text {
      margin-right: 8rpx;
    }
    
    .uni-icons {
      margin-left: 4rpx;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}

.contract-list {
  padding: 20rpx;
  height: calc(100vh - 180rpx);
  box-sizing: border-box;
}

.contract-item {
  background-color: #fff;
  margin-bottom: 20rpx;
  border-radius: 12rpx;
  overflow: visible;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  padding: 24rpx;
  transition: transform 0.2s ease;
  position: relative;
  
  &:active {
    transform: scale(0.98);
  }

  &::after {
    display: none;
  }
}

.contract-content {
  display: flex;
  flex-direction: column;
  position: relative;
  min-height: 160rpx;
  
  .contract-info {
    flex: 1;
    min-width: 0;
  }

  .contract-title {
    font-size: 28rpx;
    font-weight: 600;
    color: #333;
    margin-bottom: 16rpx;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.4;
  }

  .contract-detail {
    font-size: 28rpx;
    color: #666;
    margin-bottom: 16rpx;
    line-height: 1.5;
    display: flex;
    flex-wrap: wrap;
    gap: 16rpx;
    
    text {
      display: inline-flex;
      align-items: center;
      white-space: nowrap;
      
      &.department {
        color: #1890ff;
        font-weight: 500;
        font-size: 22rpx;
        display: flex;
        align-items: center;
        justify-content: space-between;
        
        .call-btn {
          display: inline-flex;
          align-items: center;
          margin-left: 8rpx;
          padding: 2rpx 8rpx;
          border-radius: 4rpx;
          background-color: rgba(24, 144, 255, 0.1);
          
          text {
            color: #1890ff;
            font-size: 24rpx;
            margin-left: 4rpx;
          }
        }
      }
      
      &.date, &.tenant {
        color: #666;
        font-size: 22rpx;
      }
    }
  }

  .contract-status {
    font-size: 24rpx;
    color: #999;
    line-height: 1.4;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 16rpx;
    
    text {
      display: inline-block;
      
      &.days-left {
        color: #ff6666;
        background-color: #f5f5f5;
        padding: 4rpx 12rpx;
        border-radius: 4rpx;
        font-size: 20rpx;
      }
    }
  }
}

.contract-action {
  position: absolute;
  right: 0;
  bottom: 0;
  
  .bind-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 4rpx 12rpx;
    border: 1rpx solid #999;
    border-radius: 8rpx;
    
    text {
      color: #666;
      font-size: 20rpx;
    }
  }
}

.loading,
.no-more {
  text-align: center;
  padding: 20rpx 0;
  color: #999;
  font-size: 24rpx;
}

.status-tag {
  position: absolute;
  top: 24rpx;
  right: 0;
  padding: 2rpx 8rpx;
  background-color: #13c2c2;
  color: #fff;
  font-size: 20rpx;
  border-radius: 8rpx 0 0 8rpx;
  line-height: 1.4;
}
</style>